<?php

namespace application\controller;

use application\BaseController;
use fastphp\facade\Request;
use fastphp\facade\Response;
use fastphp\facade\Validator;
use OpenApi\Annotations as OA;

class User extends BaseController
{
    /**
     * @OA\Post(
     *     path="/user/test",
     *     summary="测试接口",
     *     tags={"用户"},
     *     @OA\RequestBody(
     *         required=true,
     *         @OA\JsonContent(
     *             required={"name", "email", "age"},
     *             @OA\Property(property="name", type="string", description="用户名", minLength=3, maxLength=50),
     *             @OA\Property(property="email", type="string", description="邮箱地址", format="email"),
     *             @OA\Property(property="age", type="integer", description="年龄", minimum=18, maximum=120)
     *         )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="成功",
     *         @OA\JsonContent(
     *             @OA\Property(property="status", type="string", description="状态"),
     *             @OA\Property(property="data", type="object", description="返回数据")
     *         )
     *     ),
     *     @OA\Response(
     *         response=400,
     *         description="验证失败",
     *         @OA\JsonContent(
     *             @OA\Property(property="status", type="string", description="状态"),
     *             @OA\Property(property="errors", type="object", description="错误信息")
     *         )
     *     )
     * )
     */
    public function test()
    {
        $data = Request::post();
        $rules = [
            'name' => 'required|min:3|max:50',
            'email' => 'required|email',
            'age' => 'required|numeric|min_value:18|max_value:120'
        ];

        if (!Validator::make()->validate($data, $rules)) {
            $errors = Validator::make()->getErrors();
            // 处理错误
            return Response::create('验证失败', ['errors' => $errors], 400);
        }
    }
    
    /**
     * @OA\Post(
     *     path="/user/login",
     *     summary="用户登录",
     *     tags={"用户"},
     *     @OA\RequestBody(
     *         required=true,
     *         @OA\JsonContent(
     *             required={"username", "password"},
     *             @OA\Property(property="username", type="string", description="用户名"),
     *             @OA\Property(property="password", type="string", description="密码")
     *         )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="登录成功",
     *         @OA\JsonContent(
     *             @OA\Property(property="status", type="string", description="状态"),
     *             @OA\Property(property="token", type="string", description="访问令牌"),
     *             @OA\Property(property="user", type="object", description="用户信息")
     *         )
     *     ),
     *     @OA\Response(
     *         response=401,
     *         description="用户名或密码错误",
     *         @OA\JsonContent(
     *             @OA\Property(property="status", type="string", description="状态"),
     *             @OA\Property(property="message", type="string", description="错误信息")
     *         )
     *     )
     * )
     */
    public function login()
    {
        echo '登录';
    }
}
